package com.dawn.jar;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.*;
import java.util.Set;

/**
 * @author jiangyj
 * @version 1.0
 * @create 2021-03-05 15:01
 **/
@Mojo(name="jarEncryptionMojo",defaultPhase = LifecyclePhase.PACKAGE)
@Execute(phase = LifecyclePhase.PACKAGE)
public class JarEncryptionMojo extends AbstractMojo {

    @Parameter(property = "projectSourcePath", defaultValue = "${project.build.directory}\\classes")
    private String projectSourcePath;

    @Parameter(property = "projectOutPath", defaultValue = "${project.build.directory}")
    private String projectOutPath;


    @Parameter(property = "packagePath", defaultValue = "com")
    private String packagePath;


    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println(" ---------->> Hello jarEncryptionMojo = " + packagePath);
        // 获取所有类
        try {
            scanFile(projectSourcePath, packagePath);
        } catch (IOException e) {
            throw new MojoExecutionException("packet error", e);
        }
    }


    /**
     * 递归扫描文件,并将文件进行加密
     */
    private void scanFile(String projectSourcePath, String packageName) throws IOException {
        scanFile(projectSourcePath + '/' + packageName.replace('.', '/'));
    }

    /**
     * 递归扫描文件,并将文件进行加密
     */
    private void scanFile(String filePath) throws IOException {
        File dir = new File(filePath);
        // 递归查找所有的class文件
        for(File file : dir.listFiles()) {
            if(file.isDirectory()) {  // 如果是文件夹
                scanFile(file.getAbsolutePath());  // 进入递归 ,参数是遍历到的当前文件夹的绝对路径
            } else {
//                encryptionFile(file);
                secret(file);
            }
        }
    }




    /**
     * 加密文件
     * @param file
     */
    private void encryptionFile(File file) {
        try {
            // 转换成高级流  可以按行读
            BufferedReader reader =  new BufferedReader(new FileReader(file));
            while(reader.ready()) {
                String msg = reader.readLine();
                System.out.println(msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //加密
    public static void secret(File file) throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        int n;
        long a=System.currentTimeMillis();
        while((n=bis.read())!=-1){
            bos.write(n+1);
        }
        long b=System.currentTimeMillis();
        bos.flush();
        bis.close();
        bos.close();
        System.out.println("加密拷贝成功！");
        System.out.println("加密用时："+(b-a)+"ms");
    }


    //解密
    public static void decrypt(String str) throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(str));
        int n;
        long a=System.currentTimeMillis();
        while((n=bis.read())!=-1){
            bos.write(n-1);
        }
        long b=System.currentTimeMillis();
        bos.flush();
        bis.close();
        bos.close();
        System.out.println("解密拷贝成功！");
        System.out.println("解密用时："+(b-a)+"ms");
    }


}
